analysis

Due 12/18/2023

Author

Evan Kim



Call:
glm(formula = MondayOpenComparedToFridayCloseChangeInPercent ~ 
    FridayCloseComparedToThursdayCloseChangeInPercent + FridayIntradayChangeInPercent, 
    family = gaussian, data = df)

Coefficients:
                                                  Estimate Std. Error t value
(Intercept)                                        0.01566    0.02757   0.568
FridayCloseComparedToThursdayCloseChangeInPercent -0.11464    0.03659  -3.133
FridayIntradayChangeInPercent                      0.12679    0.04563   2.779
                                                  Pr(>|t|)   
(Intercept)                                        0.57003   
FridayCloseComparedToThursdayCloseChangeInPercent  0.00178 **
FridayIntradayChangeInPercent                      0.00557 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for gaussian family taken to be 0.6980752)

    Null deviance: 646.62  on 918  degrees of freedom
Residual deviance: 639.44  on 916  degrees of freedom
AIC: 2282.7

Number of Fisher Scoring iterations: 2

Call:
glm(formula = WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose, 
    family = binomial, data = df2)

Coefficients:
                                      Estimate Std. Error z value Pr(>|z|)    
(Intercept)                            0.34277    0.09719   3.527 0.000421 ***
didFridayCloseHigherThanThursdayClose -0.32206    0.13315  -2.419 0.015572 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1267.2  on 918  degrees of freedom
Residual deviance: 1261.3  on 917  degrees of freedom
AIC: 1265.3

Number of Fisher Scoring iterations: 4

Call:
glm(formula = WasMondayOpenHigherThanFridayClose ~ wasFridayIntradayPositive, 
    family = binomial, data = df2)

Coefficients:
                          Estimate Std. Error z value Pr(>|z|)    
(Intercept)                0.33647    0.09626   3.495 0.000473 ***
wasFridayIntradayPositive -0.31542    0.13300  -2.372 0.017711 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1267.2  on 918  degrees of freedom
Residual deviance: 1261.6  on 917  degrees of freedom
AIC: 1265.6

Number of Fisher Scoring iterations: 4

Call:
glm(formula = WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    wasFridayIntradayPositive * fridayWasATripleWitchDay, family = binomial, 
    data = df3)

Coefficients:
                                                   Estimate Std. Error z value
(Intercept)                                          0.4115     0.1110   3.707
didFridayCloseHigherThanThursdayClose               -0.2196     0.1748  -1.256
wasFridayIntradayPositive                           -0.2080     0.1771  -1.175
fridayWasATripleWitchDay                            -0.2341     0.2832  -0.827
wasFridayIntradayPositive:fridayWasATripleWitchDay   0.2518     0.5287   0.476
                                                   Pr(>|z|)    
(Intercept)                                         0.00021 ***
didFridayCloseHigherThanThursdayClose               0.20904    
wasFridayIntradayPositive                           0.24008    
fridayWasATripleWitchDay                            0.40840    
wasFridayIntradayPositive:fridayWasATripleWitchDay  0.63397    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1267.2  on 918  degrees of freedom
Residual deviance: 1259.5  on 914  degrees of freedom
AIC: 1269.5

Number of Fisher Scoring iterations: 4
# A tibble: 2 × 4
  SpecialCondition  Ones Zeros Ratio
  <chr>            <int> <int> <dbl>
1 Condition Met       35    31  1.13
2 Other              464   389  1.19

MONDAY OPEN TO FRIDAY CLOSE RATIO DENSITY PLOT & MONDAY CLOSE TO FRIDAY CLOSE RATIO DENSITY PLOT

df4 <- read.csv('data/addingFOMC/SPY_ready_for_analysis_triple_witch_NFP_CSI_FOMC_with_month.csv', header = TRUE)
df4$MondayCloseToFridayCloseRatio <- df4$FollowingWeekMondayClose / df4$PrecedingWeekFridayClose

ggplot(df4, aes(x = MondayOpenToFridayCloseRatio)) + 
    geom_density(fill = "blue", alpha = 0.5) +
  scale_x_continuous(trans = 'log', limits = c(0.95, 1.05)) +
    labs(title = "Density of Monday Open to Friday Close Ratio",
         x = "Monday Open to Friday Close Ratio",
         y = "Density")

ggplot(df4, aes(x = MondayCloseToFridayCloseRatio)) + 
    geom_density(fill = "pink", alpha = 0.5) +
    scale_x_continuous(trans = 'log', limits = c(0.95, 1.05)) +
    labs(title = "Density of Monday Close to Friday Close Ratio",
         x = "Monday Close to Friday Close Ratio",
         y = "Density")

WasMondayOpenHigherThanFridayClose and WasMondayCloseHigherThanFridayClose BREAKDOWN

#1 vs 0 proportion break down table with percentages for df4$WasMondayOpenHigherThanFridayClose and df4$WasMondayCloseHigherThanFridayClose

# Count occurrences for WasMondayOpenHigherThanFridayClose
table_open <- table(df4$WasMondayOpenHigherThanFridayClose)

# Convert counts to proportions and then to percentages for WasMondayOpenHigherThanFridayClose
percentage_open <- prop.table(table_open) * 100

# Count occurrences for WasMondayCloseHigherThanFridayClose
table_close <- table(df4$WasMondayCloseHigherThanFridayClose)

# Convert counts to proportions and then to percentages for WasMondayCloseHigherThanFridayClose
percentage_close <- prop.table(table_close) * 100



formatted_yes_open <- sprintf("%.1f%%", percentage_open[2])
formatted_no_open <- sprintf("%.1f%%", percentage_open[1])
formatted_yes_close <- sprintf("%.1f%%", percentage_close[2])
formatted_no_close <- sprintf("%.1f%%", percentage_close[1])



breakdown_table <- data.frame(
  Category = c('Monday Open Price > Friday Close Price', 'Monday Close Price > Friday Close Price'),
  Yes = c(formatted_yes_open, formatted_yes_close),
  No = c(formatted_no_open, formatted_no_close)
  
)

library(knitr)
# Print the table
kable(breakdown_table, caption = "Proportions of Monday Open and Close vs. Friday Close", format = "html")
Proportions of Monday Open and Close vs. Friday Close
Category Yes No
Monday Open Price > Friday Close Price 54.3% 45.7%
Monday Close Price > Friday Close Price 54.2% 45.8%

WasMondayOpenHigherThanFridayClose vs NFPOutcomeIsShock BREAKDOWN

df4$NFPOutcomeIsShock <- ifelse(df4$NFPOutcome == "Shock", 1, 0)


#1. breakdown table of WasMondayOpenHigherThanFridayClose (1 vs 0) and NFPOutcomeIsShock (1 vs 0)
breakdown_table_monday_open_vs_nfp_shock <- table(df4$WasMondayOpenHigherThanFridayClose, df4$NFPOutcomeIsShock)
rownames(breakdown_table_monday_open_vs_nfp_shock) <- c("Monday Open Lower Than Friday Close", "Monday Open Higher Than Friday Close")
colnames(breakdown_table_monday_open_vs_nfp_shock) <- c("Nonfarm Payrolls Non-shock", "Nonfarm Payrolls Shock")
breakdown_table_column_percentages <- prop.table(breakdown_table_monday_open_vs_nfp_shock, 2) * 100
#print(breakdown_table_column_percentages)
breakdown_table_column_percentages_df <- as.data.frame.matrix(breakdown_table_column_percentages)

# Format each value to one decimal place with a percentage sign
breakdown_table_column_percentages_df_formatted <- data.frame(lapply(breakdown_table_column_percentages_df, function(x) sprintf("%.1f%%", x)))

# Print the formatted dataframe
#print(breakdown_table_column_percentages_df_formatted)


breakdown_table_WasMondayOpenHigherThanFridayClose_vs_NFPOutcomeIsShock <- data.frame(
  . = c("Monday Open Lower Than Friday Close", "Monday Open Higher Than Friday Close"),
  NFP_Nonshock = c(breakdown_table_column_percentages_df_formatted[1][[1]][1], breakdown_table_column_percentages_df_formatted[1][[1]][2]),
  NFP_Shock = c(breakdown_table_column_percentages_df_formatted[2][[1]][1], breakdown_table_column_percentages_df_formatted[2][[1]][2])
  
)


library(knitr)
# Print the table
kable(breakdown_table_WasMondayOpenHigherThanFridayClose_vs_NFPOutcomeIsShock, caption = "Proportion Breakdown of Monday Open Higher Than Friday Close vs Nonfarm Payroll Shock", format = "html")
Proportion Breakdown of Monday Open Higher Than Friday Close vs Nonfarm Payroll Shock
. NFP_Nonshock NFP_Shock
Monday Open Lower Than Friday Close 45.8% 41.7%
Monday Open Higher Than Friday Close 54.2% 58.3%

WasMondayCloseHigherThanFridayClose vs NFPOutcomeIsShock BREAKDOWN

df4$NFPOutcomeIsShock <- ifelse(df4$NFPOutcome == "Shock", 1, 0)


#1. breakdown table of WasMondayCloseHigherThanFridayClose (1 vs 0) and NFPOutcomeIsShock (1 vs 0)
breakdown_table_monday_close_vs_nfp_shock <- table(df4$WasMondayCloseHigherThanFridayClose, df4$NFPOutcomeIsShock)
rownames(breakdown_table_monday_close_vs_nfp_shock) <- c("Monday Close Lower Than Friday Close", "Monday Close Higher Than Friday Close")
colnames(breakdown_table_monday_close_vs_nfp_shock) <- c("Nonfarm Payrolls Non-shock", "Nonfarm Payrolls Shock")
breakdown_table_column_percentages2 <- prop.table(breakdown_table_monday_close_vs_nfp_shock, 2) * 100
#print(breakdown_table_column_percentages)
breakdown_table_column_percentages_df2 <- as.data.frame.matrix(breakdown_table_column_percentages2)

# Format each value to one decimal place with a percentage sign
breakdown_table_column_percentages_df_formatted2 <- data.frame(lapply(breakdown_table_column_percentages_df2, function(x) sprintf("%.1f%%", x)))

# Print the formatted dataframe
#print(breakdown_table_column_percentages_df_formatted)


breakdown_table_WasMondayCloseHigherThanFridayClose_vs_NFPOutcomeIsShock <- data.frame(
  . = c("Monday Open Lower Than Friday Close", "Monday Open Higher Than Friday Close"),
  NFP_Nonshock = c(breakdown_table_column_percentages_df_formatted2[1][[1]][1], breakdown_table_column_percentages_df_formatted2[1][[1]][2]),
  NFP_Shock = c(breakdown_table_column_percentages_df_formatted2[2][[1]][1], breakdown_table_column_percentages_df_formatted2[2][[1]][2])
  
)


library(knitr)
# Print the table
kable(breakdown_table_WasMondayCloseHigherThanFridayClose_vs_NFPOutcomeIsShock, caption = "Proportion Breakdown of Monday Close Higher Than Friday Close vs Nonfarm Payroll Shock", format = "html")
Proportion Breakdown of Monday Close Higher Than Friday Close vs Nonfarm Payroll Shock
. NFP_Nonshock NFP_Shock
Monday Open Lower Than Friday Close 45.4% 75.0%
Monday Open Higher Than Friday Close 54.6% 25.0%
Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
ℹ Please use tidy evaluation idioms with `aes()`.
ℹ See also `vignette("ggplot2-in-packages")` for more information.

Warning: Removed 614 rows containing missing values (`geom_point()`).

[[1]]


[[2]]


[[3]]


[[4]]


[[5]]


[[6]]


[[7]]


[[8]]


[[9]]


[[10]]


[[11]]


[[12]]


[[13]]


[[14]]


[[15]]


[[16]]


[[17]]


[[18]]


[[19]]


[[20]]


[[21]]


[[22]]


[[23]]


[[24]]


[[25]]


[[26]]


[[27]]


[[28]]


[[29]]


[[30]]


[[31]]


[[32]]


[[33]]


[[34]]


[[35]]


[[36]]


[[37]]
Warning: Removed 614 rows containing missing values (`geom_point()`).


[[38]]


[[39]]


[[40]]


[[41]]


[[42]]


[[43]]


[[44]]


[[45]]


[[46]]

df6 <- read.csv('data/addingUmichSentiment/SPY_ready_for_analysis_triple_witch_NFP_CSI.csv', header = TRUE)
summary(df6$CSIActualMinusExpected)
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max.     NA's 
-11.0000  -1.4000   0.1000  -0.2161   1.2000   9.1000      614 
table(df6$WasMondayOpenHigherThanFridayClose)

  0   1 
420 499 
boxplot(CSIActualMinusExpected ~ WasMondayOpenHigherThanFridayClose, data = df6, main = "CSIActualMinusExpected vs WasMondayOpenHigherThanFridayClose")

library(ggplot2)
ggplot(df6, aes(x = CSIActualMinusExpected, fill = factor(WasMondayOpenHigherThanFridayClose))) + 
  geom_density(alpha = 0.5) +
  labs(fill = "WasMondayOpenHigher")
Warning: Removed 614 rows containing non-finite values (`stat_density()`).

# T-test or Mann-Whitney U Test (depending on data distribution)
t.test(CSIActualMinusExpected ~ WasMondayOpenHigherThanFridayClose, data = df6)

    Welch Two Sample t-test

data:  CSIActualMinusExpected by WasMondayOpenHigherThanFridayClose
t = -0.56092, df = 287.94, p-value = 0.5753
alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
95 percent confidence interval:
 -0.8476626  0.4716685
sample estimates:
mean in group 0 mean in group 1 
     -0.3128378      -0.1248408 
df6$CSIOutcomeIsShock <- ifelse(df6$CSIOutcome == "Shock", 1, 0)
table(df6$CSIOutcomeIsShock, df6$WasMondayOpenHigherThanFridayClose)
   
      0   1
  0 404 489
  1  16  10
chisq.test(table(df6$CSIOutcomeIsShock, df6$WasMondayOpenHigherThanFridayClose))

    Pearson's Chi-squared test with Yates' continuity correction

data:  table(df6$CSIOutcomeIsShock, df6$WasMondayOpenHigherThanFridayClose)
X-squared = 2.0873, df = 1, p-value = 0.1485
df6 <- read.csv('data/addingUmichSentiment/SPY_ready_for_analysis_triple_witch_NFP_CSI.csv', header = TRUE)
df6$NFPOutcomeIsShock <- ifelse(df6$NFPOutcome == "Shock", 1, 0)
df6$CSIOutcomeIsShock <- ifelse(df6$CSIOutcome == "Shock", 1, 0)

# Binomial model

model6 <- glm(WasMondayCloseHigherThanFridayClose ~ wasFridayIntradayPositive + NFPOutcomeIsShock + fridayWasATripleWitchDay + CSIOutcomeIsShock, 
                      data = df6, family = binomial)


# Print the summary of the model
summary(model6)

Call:
glm(formula = WasMondayCloseHigherThanFridayClose ~ wasFridayIntradayPositive + 
    NFPOutcomeIsShock + fridayWasATripleWitchDay + CSIOutcomeIsShock, 
    family = binomial, data = df6)

Coefficients:
                          Estimate Std. Error z value Pr(>|z|)   
(Intercept)                0.32631    0.10244   3.185  0.00145 **
wasFridayIntradayPositive -0.18199    0.13519  -1.346  0.17825   
NFPOutcomeIsShock         -1.28997    0.67133  -1.922  0.05467 . 
fridayWasATripleWitchDay  -0.52945    0.23946  -2.211  0.02703 * 
CSIOutcomeIsShock         -0.06743    0.40024  -0.168  0.86621   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1267.5  on 918  degrees of freedom
Residual deviance: 1257.3  on 914  degrees of freedom
AIC: 1267.3

Number of Fisher Scoring iterations: 4
df7 <- read.csv('data/addingFOMC/SPY_ready_for_analysis_triple_witch_NFP_CSI_FOMC.csv', header = TRUE)



df7$NFPOutcomeIsShock <- ifelse(df7$NFPOutcome == "Shock", 1, 0)
df7$CSIOutcomeIsShock <- ifelse(df7$CSIOutcome == "Shock", 1, 0)



df7_filtered <- rbind(df7[1:220, ], df7[264:nrow(df7), ])
df7_filtered$NFPOutcomeIsShock <- ifelse(df7_filtered$NFPOutcome == "Shock", 1, 0)
df7_filtered$CSIOutcomeIsShock <- ifelse(df7_filtered$CSIOutcome == "Shock", 1, 0)
df7_filtered$PrecedingWeekFridayVolumeMINUSPrecedingWeekThursdayVolume <- (df7_filtered$PrecedingWeekFridayVolume) - (df7_filtered$PrecedingWeekThursdayVolume)
#df7$FridayCloseToFridayOpenRatio <- (df$PrecedingWeekFridayClose) / (df$PrecedingWeekFridayOpen)


model7BUYFRICLOSESELLMONOPEN <- glm(WasMondayOpenHigherThanFridayClose ~ wasFridayIntradayPositive, 
                      data = df7_filtered, family = binomial)
summary(model7BUYFRICLOSESELLMONOPEN)

Call:
glm(formula = WasMondayOpenHigherThanFridayClose ~ wasFridayIntradayPositive, 
    family = binomial, data = df7_filtered)

Coefficients:
                          Estimate Std. Error z value Pr(>|z|)    
(Intercept)                0.33647    0.09897   3.400 0.000675 ***
wasFridayIntradayPositive -0.32770    0.13626  -2.405 0.016178 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1208.5  on 875  degrees of freedom
Residual deviance: 1202.7  on 874  degrees of freedom
AIC: 1206.7

Number of Fisher Scoring iterations: 4
# wasFridayIntradayPositive
#model7BUYFRICLOSESELLMONclose <- glm(WasMondayCloseHigherThanFridayClose ~ NFPOutcomeIsShock + fridayWasATripleWitchDay, 
#                      data = df7_filtered, family = binomial)
  

#summary(model7BUYFRICLOSESELLMONclose)
df8 <- read.csv('data/addingFOMC/SPY_ready_for_analysis_triple_witch_NFP_CSI_FOMC_with_month.csv', header = TRUE)
#df8 <- rbind(df8[1:220, ], df8[264:nrow(df8), ])
df8$NFPOutcomeIsShock <- ifelse(df8$NFPOutcome == "Shock", 1, 0)
df8$CSIOutcomeIsShock <- ifelse(df8$CSIOutcome == "Shock", 1, 0)
df8$PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO <- (df8$PrecedingWeekFridayVolume) / (df8$PrecedingWeekThursdayVolume)


null_model <- glm(WasMondayOpenHigherThanFridayClose ~ 1, 
                  data = df8, family = binomial)
summary(null_model)

Call:
glm(formula = WasMondayOpenHigherThanFridayClose ~ 1, family = binomial, 
    data = df8)

Coefficients:
            Estimate Std. Error z value Pr(>|z|)   
(Intercept)  0.17235    0.06622   2.603  0.00925 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1267.2  on 918  degrees of freedom
Residual deviance: 1267.2  on 918  degrees of freedom
AIC: 1269.2

Number of Fisher Scoring iterations: 3
full_model <- glm(WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + FridayIntradayChangeInPercent + fridayWasATripleWitchDay + NFPDayOrNot + NFPOutcome + CSIOutcome + CSIDayOrNot + wasItFOMCWeek + FOMCAction + FOMCActualVSExpected + FOMCActionSpecial + NFPOutcomeIsShock + CSIOutcomeIsShock + PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO, 
                  data = df8, family = binomial)
summary(full_model)

Call:
glm(formula = WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + fridayWasATripleWitchDay + 
    NFPDayOrNot + NFPOutcome + CSIOutcome + CSIDayOrNot + wasItFOMCWeek + 
    FOMCAction + FOMCActualVSExpected + FOMCActionSpecial + NFPOutcomeIsShock + 
    CSIOutcomeIsShock + PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO, 
    family = binomial, data = df8)

Coefficients: (7 not defined because of singularities)
                                                           Estimate Std. Error
(Intercept)                                                 0.79139    0.27674
didFridayCloseHigherThanThursdayClose                      -0.36570    0.21345
FridayCloseComparedToThursdayCloseChangeInPercent           0.05392    0.10829
wasFridayIntradayPositive                                  -0.41402    0.21009
FridayIntradayChangeInPercent                               0.19213    0.13439
fridayWasATripleWitchDay                                   -0.28388    0.25759
NFPDayOrNot                                                -0.27942    0.18926
NFPOutcomeShock                                             0.52127    0.64363
NFPOutcomeUpbeat                                            0.40145    0.64233
CSIOutcomeBaseline                                         -0.15524    0.15692
CSIOutcomeShock                                            -1.03298    0.39505
CSIOutcomeUpbeat                                            0.13896    0.65833
CSIDayOrNot                                                      NA         NA
wasItFOMCWeek                                              -0.83568    0.87603
FOMCActionCut                                              -0.65468    1.42167
FOMCActionHike                                              1.37253    1.01127
FOMCActionPause                                             1.11306    0.89877
FOMCActionSuperCut                                          0.50344    1.68962
FOMCActionSuperHike                                              NA         NA
FOMCActualVSExpectedDovish                                -14.03440  535.41356
FOMCActualVSExpectedHawkish                                12.68100  535.41148
FOMCActualVSExpectedMatch                                        NA         NA
FOMCActionSpecialSuperCut                                        NA         NA
FOMCActionSpecialSuperHike                                       NA         NA
NFPOutcomeIsShock                                                NA         NA
CSIOutcomeIsShock                                                NA         NA
PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO  -0.07900    0.20513
                                                          z value Pr(>|z|)   
(Intercept)                                                 2.860  0.00424 **
didFridayCloseHigherThanThursdayClose                      -1.713  0.08666 . 
FridayCloseComparedToThursdayCloseChangeInPercent           0.498  0.61850   
wasFridayIntradayPositive                                  -1.971  0.04876 * 
FridayIntradayChangeInPercent                               1.430  0.15283   
fridayWasATripleWitchDay                                   -1.102  0.27043   
NFPDayOrNot                                                -1.476  0.13985   
NFPOutcomeShock                                             0.810  0.41800   
NFPOutcomeUpbeat                                            0.625  0.53198   
CSIOutcomeBaseline                                         -0.989  0.32251   
CSIOutcomeShock                                            -2.615  0.00893 **
CSIOutcomeUpbeat                                            0.211  0.83282   
CSIDayOrNot                                                    NA       NA   
wasItFOMCWeek                                              -0.954  0.34011   
FOMCActionCut                                              -0.460  0.64516   
FOMCActionHike                                              1.357  0.17471   
FOMCActionPause                                             1.238  0.21556   
FOMCActionSuperCut                                          0.298  0.76573   
FOMCActionSuperHike                                            NA       NA   
FOMCActualVSExpectedDovish                                 -0.026  0.97909   
FOMCActualVSExpectedHawkish                                 0.024  0.98110   
FOMCActualVSExpectedMatch                                      NA       NA   
FOMCActionSpecialSuperCut                                      NA       NA   
FOMCActionSpecialSuperHike                                     NA       NA   
NFPOutcomeIsShock                                              NA       NA   
CSIOutcomeIsShock                                              NA       NA   
PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO  -0.385  0.70013   
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1267.2  on 918  degrees of freedom
Residual deviance: 1236.1  on 899  degrees of freedom
AIC: 1276.1

Number of Fisher Scoring iterations: 12
forward_model <- step(null_model, 
                      scope = list(lower = null_model, upper = full_model), 
                      direction = "forward")
Start:  AIC=1269.21
WasMondayOpenHigherThanFridayClose ~ 1

                                                            Df Deviance    AIC
+ CSIOutcomeIsShock                                          1   1260.0 1264.0
+ didFridayCloseHigherThanThursdayClose                      1   1261.3 1265.3
+ wasFridayIntradayPositive                                  1   1261.6 1265.6
+ CSIOutcome                                                 3   1259.6 1267.6
<none>                                                           1267.2 1269.2
+ CSIDayOrNot                                                1   1265.7 1269.7
+ NFPDayOrNot                                                1   1265.9 1269.9
+ FridayCloseComparedToThursdayCloseChangeInPercent          1   1266.7 1270.7
+ wasItFOMCWeek                                              1   1266.9 1270.9
+ FridayIntradayChangeInPercent                              1   1267.0 1271.0
+ NFPOutcomeIsShock                                          1   1267.1 1271.1
+ fridayWasATripleWitchDay                                   1   1267.2 1271.2
+ PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO  1   1267.2 1271.2
+ FOMCActionSpecial                                          2   1265.6 1271.6
+ FOMCActualVSExpected                                       3   1264.1 1272.1
+ NFPOutcome                                                 2   1266.7 1272.7
+ FOMCAction                                                 5   1261.1 1273.1

Step:  AIC=1264.04
WasMondayOpenHigherThanFridayClose ~ CSIOutcomeIsShock

                                                            Df Deviance    AIC
+ wasFridayIntradayPositive                                  1   1254.4 1260.4
+ didFridayCloseHigherThanThursdayClose                      1   1254.5 1260.5
<none>                                                           1260.0 1264.0
+ NFPDayOrNot                                                1   1258.3 1264.3
+ FridayCloseComparedToThursdayCloseChangeInPercent          1   1259.5 1265.5
+ CSIDayOrNot                                                1   1259.8 1265.8
+ FridayIntradayChangeInPercent                              1   1259.8 1265.8
+ wasItFOMCWeek                                              1   1259.8 1265.8
+ NFPOutcomeIsShock                                          1   1260.0 1266.0
+ fridayWasATripleWitchDay                                   1   1260.0 1266.0
+ PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO  1   1260.0 1266.0
+ FOMCActionSpecial                                          2   1258.3 1266.3
+ FOMCActualVSExpected                                       3   1257.0 1267.0
+ CSIOutcome                                                 2   1259.6 1267.6
+ NFPOutcome                                                 2   1259.7 1267.7
+ FOMCAction                                                 5   1254.0 1268.0

Step:  AIC=1260.42
WasMondayOpenHigherThanFridayClose ~ CSIOutcomeIsShock + wasFridayIntradayPositive

                                                            Df Deviance    AIC
+ FridayIntradayChangeInPercent                              1   1251.5 1259.5
<none>                                                           1254.4 1260.4
+ NFPDayOrNot                                                1   1253.1 1261.1
+ didFridayCloseHigherThanThursdayClose                      1   1253.2 1261.2
+ FridayCloseComparedToThursdayCloseChangeInPercent          1   1253.9 1261.9
+ PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO  1   1254.1 1262.1
+ fridayWasATripleWitchDay                                   1   1254.1 1262.1
+ CSIDayOrNot                                                1   1254.2 1262.2
+ wasItFOMCWeek                                              1   1254.3 1262.3
+ NFPOutcomeIsShock                                          1   1254.3 1262.3
+ FOMCActionSpecial                                          2   1252.8 1262.8
+ FOMCActualVSExpected                                       3   1251.8 1263.8
+ NFPOutcome                                                 2   1253.9 1263.9
+ CSIOutcome                                                 2   1254.0 1264.0
+ FOMCAction                                                 5   1248.3 1264.3

Step:  AIC=1259.54
WasMondayOpenHigherThanFridayClose ~ CSIOutcomeIsShock + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent

                                                            Df Deviance    AIC
+ didFridayCloseHigherThanThursdayClose                      1   1249.2 1259.2
<none>                                                           1251.5 1259.5
+ NFPDayOrNot                                                1   1250.2 1260.2
+ fridayWasATripleWitchDay                                   1   1251.3 1261.3
+ CSIDayOrNot                                                1   1251.3 1261.3
+ wasItFOMCWeek                                              1   1251.4 1261.4
+ FridayCloseComparedToThursdayCloseChangeInPercent          1   1251.4 1261.4
+ PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO  1   1251.4 1261.4
+ NFPOutcomeIsShock                                          1   1251.4 1261.4
+ FOMCActionSpecial                                          2   1249.6 1261.6
+ FOMCAction                                                 5   1244.6 1262.6
+ FOMCActualVSExpected                                       3   1248.8 1262.8
+ NFPOutcome                                                 2   1251.0 1263.0
+ CSIOutcome                                                 2   1251.2 1263.2

Step:  AIC=1259.19
WasMondayOpenHigherThanFridayClose ~ CSIOutcomeIsShock + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + didFridayCloseHigherThanThursdayClose

                                                            Df Deviance    AIC
<none>                                                           1249.2 1259.2
+ NFPDayOrNot                                                1   1247.9 1259.9
+ fridayWasATripleWitchDay                                   1   1248.7 1260.7
+ PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO  1   1248.9 1260.9
+ CSIDayOrNot                                                1   1248.9 1260.9
+ FridayCloseComparedToThursdayCloseChangeInPercent          1   1248.9 1260.9
+ FOMCActionSpecial                                          2   1247.1 1261.1
+ wasItFOMCWeek                                              1   1249.1 1261.1
+ NFPOutcomeIsShock                                          1   1249.1 1261.1
+ FOMCActualVSExpected                                       3   1246.4 1262.4
+ FOMCAction                                                 5   1242.5 1262.5
+ NFPOutcome                                                 2   1248.6 1262.6
+ CSIOutcome                                                 2   1248.7 1262.7
summary(forward_model)

Call:
glm(formula = WasMondayOpenHigherThanFridayClose ~ CSIOutcomeIsShock + 
    wasFridayIntradayPositive + FridayIntradayChangeInPercent + 
    didFridayCloseHigherThanThursdayClose, family = binomial, 
    data = df8)

Coefficients:
                                      Estimate Std. Error z value Pr(>|z|)    
(Intercept)                             0.5668     0.1313   4.317 1.59e-05 ***
CSIOutcomeIsShock                      -0.9661     0.3891  -2.483   0.0130 *  
wasFridayIntradayPositive              -0.4103     0.2047  -2.004   0.0451 *  
FridayIntradayChangeInPercent           0.2150     0.1084   1.983   0.0474 *  
didFridayCloseHigherThanThursdayClose  -0.2738     0.1786  -1.533   0.1252    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1267.2  on 918  degrees of freedom
Residual deviance: 1249.2  on 914  degrees of freedom
AIC: 1259.2

Number of Fisher Scoring iterations: 4
backward_model <- step(full_model, direction = "backward")
Start:  AIC=1276.08
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + fridayWasATripleWitchDay + 
    NFPDayOrNot + NFPOutcome + CSIOutcome + CSIDayOrNot + wasItFOMCWeek + 
    FOMCAction + FOMCActualVSExpected + FOMCActionSpecial + NFPOutcomeIsShock + 
    CSIOutcomeIsShock + PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO


Step:  AIC=1276.08
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + fridayWasATripleWitchDay + 
    NFPDayOrNot + NFPOutcome + CSIOutcome + CSIDayOrNot + wasItFOMCWeek + 
    FOMCAction + FOMCActualVSExpected + FOMCActionSpecial + NFPOutcomeIsShock + 
    PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO


Step:  AIC=1276.08
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + fridayWasATripleWitchDay + 
    NFPDayOrNot + NFPOutcome + CSIOutcome + CSIDayOrNot + wasItFOMCWeek + 
    FOMCAction + FOMCActualVSExpected + FOMCActionSpecial + PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO


Step:  AIC=1276.08
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + fridayWasATripleWitchDay + 
    NFPDayOrNot + NFPOutcome + CSIOutcome + CSIDayOrNot + wasItFOMCWeek + 
    FOMCAction + FOMCActualVSExpected + PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO


Step:  AIC=1276.08
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + fridayWasATripleWitchDay + 
    NFPDayOrNot + NFPOutcome + CSIOutcome + CSIDayOrNot + FOMCAction + 
    FOMCActualVSExpected + PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO


Step:  AIC=1276.08
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + fridayWasATripleWitchDay + 
    NFPDayOrNot + NFPOutcome + CSIOutcome + FOMCAction + FOMCActualVSExpected + 
    PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO

                                                            Df Deviance    AIC
- NFPOutcome                                                 2   1237.1 1273.1
- FOMCAction                                                 4   1241.2 1273.2
- FOMCActualVSExpected                                       2   1238.0 1274.0
- PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO  1   1236.2 1274.2
- FridayCloseComparedToThursdayCloseChangeInPercent          1   1236.3 1274.3
- fridayWasATripleWitchDay                                   1   1237.3 1275.3
<none>                                                           1236.1 1276.1
- FridayIntradayChangeInPercent                              1   1238.2 1276.2
- NFPDayOrNot                                                1   1238.3 1276.3
- didFridayCloseHigherThanThursdayClose                      1   1239.0 1277.0
- wasFridayIntradayPositive                                  1   1240.0 1278.0
- CSIOutcome                                                 3   1244.0 1278.0

Step:  AIC=1273.13
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + fridayWasATripleWitchDay + 
    NFPDayOrNot + CSIOutcome + FOMCAction + FOMCActualVSExpected + 
    PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO

                                                            Df Deviance    AIC
- FOMCAction                                                 4   1242.3 1270.3
- FOMCActualVSExpected                                       2   1238.8 1270.8
- PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO  1   1237.3 1271.3
- FridayCloseComparedToThursdayCloseChangeInPercent          1   1237.4 1271.4
- fridayWasATripleWitchDay                                   1   1238.4 1272.4
- NFPDayOrNot                                                1   1238.9 1272.9
<none>                                                           1237.1 1273.1
- FridayIntradayChangeInPercent                              1   1239.3 1273.3
- didFridayCloseHigherThanThursdayClose                      1   1240.1 1274.1
- wasFridayIntradayPositive                                  1   1240.9 1274.9
- CSIOutcome                                                 3   1245.3 1275.3

Step:  AIC=1270.35
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + fridayWasATripleWitchDay + 
    NFPDayOrNot + CSIOutcome + FOMCActualVSExpected + PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO

                                                            Df Deviance    AIC
- FOMCActualVSExpected                                       3   1245.8 1267.8
- PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO  1   1242.5 1268.5
- FridayCloseComparedToThursdayCloseChangeInPercent          1   1242.7 1268.7
- fridayWasATripleWitchDay                                   1   1243.5 1269.5
- FridayIntradayChangeInPercent                              1   1244.2 1270.2
<none>                                                           1242.3 1270.3
- NFPDayOrNot                                                1   1244.6 1270.6
- wasFridayIntradayPositive                                  1   1245.5 1271.5
- didFridayCloseHigherThanThursdayClose                      1   1245.7 1271.7
- CSIOutcome                                                 3   1250.4 1272.4

Step:  AIC=1267.78
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + fridayWasATripleWitchDay + 
    NFPDayOrNot + CSIOutcome + PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO

                                                            Df Deviance    AIC
- PrecedingWeekFridayVolumePrecedingWeekThursdayVolumeRATIO  1   1245.9 1265.9
- FridayCloseComparedToThursdayCloseChangeInPercent          1   1246.0 1266.0
- fridayWasATripleWitchDay                                   1   1246.6 1266.6
- FridayIntradayChangeInPercent                              1   1247.6 1267.6
- NFPDayOrNot                                                1   1247.7 1267.7
<none>                                                           1245.8 1267.8
- didFridayCloseHigherThanThursdayClose                      1   1248.8 1268.8
- wasFridayIntradayPositive                                  1   1249.1 1269.1
- CSIOutcome                                                 3   1253.8 1269.8

Step:  AIC=1265.88
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    FridayCloseComparedToThursdayCloseChangeInPercent + wasFridayIntradayPositive + 
    FridayIntradayChangeInPercent + fridayWasATripleWitchDay + 
    NFPDayOrNot + CSIOutcome

                                                    Df Deviance    AIC
- FridayCloseComparedToThursdayCloseChangeInPercent  1   1246.2 1264.2
- fridayWasATripleWitchDay                           1   1246.7 1264.7
- FridayIntradayChangeInPercent                      1   1247.7 1265.7
<none>                                                   1245.9 1265.9
- NFPDayOrNot                                        1   1248.0 1266.0
- didFridayCloseHigherThanThursdayClose              1   1248.9 1266.9
- wasFridayIntradayPositive                          1   1249.2 1267.2
- CSIOutcome                                         3   1254.0 1268.0

Step:  AIC=1264.15
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    wasFridayIntradayPositive + FridayIntradayChangeInPercent + 
    fridayWasATripleWitchDay + NFPDayOrNot + CSIOutcome

                                        Df Deviance    AIC
- fridayWasATripleWitchDay               1   1247.0 1263.0
<none>                                       1246.2 1264.2
- NFPDayOrNot                            1   1248.2 1264.2
- didFridayCloseHigherThanThursdayClose  1   1249.1 1265.1
- wasFridayIntradayPositive              1   1249.8 1265.8
- FridayIntradayChangeInPercent          1   1250.2 1266.2
- CSIOutcome                             3   1254.3 1266.3

Step:  AIC=1263.01
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    wasFridayIntradayPositive + FridayIntradayChangeInPercent + 
    NFPDayOrNot + CSIOutcome

                                        Df Deviance    AIC
- NFPDayOrNot                            1   1248.7 1262.7
<none>                                       1247.0 1263.0
- didFridayCloseHigherThanThursdayClose  1   1249.5 1263.5
- wasFridayIntradayPositive              1   1250.6 1264.6
- CSIOutcome                             3   1255.0 1265.0
- FridayIntradayChangeInPercent          1   1251.0 1265.0

Step:  AIC=1262.71
WasMondayOpenHigherThanFridayClose ~ didFridayCloseHigherThanThursdayClose + 
    wasFridayIntradayPositive + FridayIntradayChangeInPercent + 
    CSIOutcome

                                        Df Deviance    AIC
<none>                                       1248.7 1262.7
- didFridayCloseHigherThanThursdayClose  1   1251.2 1263.2
- CSIOutcome                             3   1255.9 1263.9
- wasFridayIntradayPositive              1   1252.6 1264.6
- FridayIntradayChangeInPercent          1   1252.7 1264.7
summary(forward_model)

Call:
glm(formula = WasMondayOpenHigherThanFridayClose ~ CSIOutcomeIsShock + 
    wasFridayIntradayPositive + FridayIntradayChangeInPercent + 
    didFridayCloseHigherThanThursdayClose, family = binomial, 
    data = df8)

Coefficients:
                                      Estimate Std. Error z value Pr(>|z|)    
(Intercept)                             0.5668     0.1313   4.317 1.59e-05 ***
CSIOutcomeIsShock                      -0.9661     0.3891  -2.483   0.0130 *  
wasFridayIntradayPositive              -0.4103     0.2047  -2.004   0.0451 *  
FridayIntradayChangeInPercent           0.2150     0.1084   1.983   0.0474 *  
didFridayCloseHigherThanThursdayClose  -0.2738     0.1786  -1.533   0.1252    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1267.2  on 918  degrees of freedom
Residual deviance: 1249.2  on 914  degrees of freedom
AIC: 1259.2

Number of Fisher Scoring iterations: 4
non_random_model <- glm(formula = WasMondayOpenHigherThanFridayClose ~ CSIOutcomeIsShock + 
    wasFridayIntradayPositive + FridayIntradayChangeInPercent + 
    didFridayCloseHigherThanThursdayClose, family = binomial, 
    data = df8)
summary(non_random_model)

Call:
glm(formula = WasMondayOpenHigherThanFridayClose ~ CSIOutcomeIsShock + 
    wasFridayIntradayPositive + FridayIntradayChangeInPercent + 
    didFridayCloseHigherThanThursdayClose, family = binomial, 
    data = df8)

Coefficients:
                                      Estimate Std. Error z value Pr(>|z|)    
(Intercept)                             0.5668     0.1313   4.317 1.59e-05 ***
CSIOutcomeIsShock                      -0.9661     0.3891  -2.483   0.0130 *  
wasFridayIntradayPositive              -0.4103     0.2047  -2.004   0.0451 *  
FridayIntradayChangeInPercent           0.2150     0.1084   1.983   0.0474 *  
didFridayCloseHigherThanThursdayClose  -0.2738     0.1786  -1.533   0.1252    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 1267.2  on 918  degrees of freedom
Residual deviance: 1249.2  on 914  degrees of freedom
AIC: 1259.2

Number of Fisher Scoring iterations: 4

why PrecedingWeekFridayMonth should be random effect

df8$PrecedingWeekFridayMonth <- factor(df8$PrecedingWeekFridayMonth, levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))

table(df8$PrecedingWeekFridayMonth)

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 
 65  64  87  75  71  87  79  84  78  92  68  69 
aggregate(WasMondayOpenHigherThanFridayClose ~ PrecedingWeekFridayMonth, data = df8, mean)
   PrecedingWeekFridayMonth WasMondayOpenHigherThanFridayClose
1                       Jan                          0.4923077
2                       Feb                          0.5312500
3                       Mar                          0.4827586
4                       Apr                          0.5866667
5                       May                          0.4929577
6                       Jun                          0.5747126
7                       Jul                          0.5696203
8                       Aug                          0.5595238
9                       Sep                          0.4615385
10                      Oct                          0.5869565
11                      Nov                          0.5441176
12                      Dec                          0.6231884
library(ggplot2)
ggplot(df8, aes(x = PrecedingWeekFridayMonth, fill = factor(WasMondayOpenHigherThanFridayClose))) +
  geom_bar(position = "fill") +
  ylab("Proportion")

contingency_table_months <- table(df8$PrecedingWeekFridayMonth, df8$WasMondayOpenHigherThanFridayClose)
print(contingency_table_months)
     
       0  1
  Jan 33 32
  Feb 30 34
  Mar 45 42
  Apr 31 44
  May 36 35
  Jun 37 50
  Jul 34 45
  Aug 37 47
  Sep 42 36
  Oct 38 54
  Nov 31 37
  Dec 26 43
# Perform the Chi-square test
chi_sq_test_months <- chisq.test(contingency_table_months)

# Output the result
chi_sq_test_months

    Pearson's Chi-squared test

data:  contingency_table_months
X-squared = 8.5349, df = 11, p-value = 0.6647

chi-squared

# Assign new groupings based on seasons
df8$Month <- with(df8, factor(
  ifelse(PrecedingWeekFridayMonth %in% c("Mar", "Jan", "May", "Sept"), "Odd", "Even")))


# Create a new contingency table with the season groupings
contingency_table_Month <- table(df8$Month, df8$WasMondayOpenHigherThanFridayClose)

# Perform the Chi-square test with the new grouping
chi_sq_test_Month <- chisq.test(contingency_table_Month)

# Output the result
chi_sq_test_Month

    Pearson's Chi-squared test with Yates' continuity correction

data:  contingency_table_Month
X-squared = 3.2023, df = 1, p-value = 0.07353
library(lme4)
Loading required package: Matrix

Attaching package: 'Matrix'
The following objects are masked from 'package:tidyr':

    expand, pack, unpack
mixed_model <- glmer(WasMondayOpenHigherThanFridayClose ~ CSIOutcomeIsShock + 
                     wasFridayIntradayPositive + FridayIntradayChangeInPercent + 
                     didFridayCloseHigherThanThursdayClose + 
                     (1 | Month), 
                     family = binomial, data = df8)
summary(mixed_model)
Generalized linear mixed model fit by maximum likelihood (Laplace
  Approximation) [glmerMod]
 Family: binomial  ( logit )
Formula: 
WasMondayOpenHigherThanFridayClose ~ CSIOutcomeIsShock + wasFridayIntradayPositive +  
    FridayIntradayChangeInPercent + didFridayCloseHigherThanThursdayClose +  
    (1 | Month)
   Data: df8

     AIC      BIC   logLik deviance df.resid 
  1260.9   1289.9   -624.5   1248.9      913 

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-1.6045 -1.0395  0.7570  0.9028  1.6786 

Random effects:
 Groups Name        Variance Std.Dev.
 Month  (Intercept) 0.008371 0.09149 
Number of obs: 919, groups:  Month, 2

Fixed effects:
                                      Estimate Std. Error z value Pr(>|z|)    
(Intercept)                             0.5359     0.1561   3.433 0.000596 ***
CSIOutcomeIsShock                      -0.9742     0.3895  -2.501 0.012385 *  
wasFridayIntradayPositive              -0.4109     0.2050  -2.005 0.044991 *  
FridayIntradayChangeInPercent           0.2142     0.1086   1.972 0.048559 *  
didFridayCloseHigherThanThursdayClose  -0.2733     0.1788  -1.529 0.126307    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
            (Intr) CSIOIS wsFrIP FrICIP
CSIOtcmIsSh -0.051                     
wsFrdyIntrP -0.449  0.006              
FrdyIntrCIP  0.522  0.021 -0.531       
ddFrdyCHTTC -0.338 -0.026 -0.399 -0.234
library(lme4)

anova(non_random_model, mixed_model)
Analysis of Deviance Table

Model: binomial, link: logit

Response: WasMondayOpenHigherThanFridayClose

Terms added sequentially (first to last)

                                      Df Deviance Resid. Df Resid. Dev
NULL                                                    918     1267.2
CSIOutcomeIsShock                      1   7.1648       917     1260.0
wasFridayIntradayPositive              1   5.6186       916     1254.4
FridayIntradayChangeInPercent          1   2.8772       915     1251.5
didFridayCloseHigherThanThursdayClose  1   2.3515       914     1249.2
#summary(glm(formula = WasMondayOpenHigherThanFridayClose ~ CSIOutcomeIsShock + 
#    wasFridayIntradayPositive + FridayIntradayChangeInPercent, family = binomial, 
#    data = df8))
#create df8$non_random_model_predict and df8$mixed_model_predict by going through each row of df8
# Predict with the non-random model
df8$non_random_model_predict <- as.integer(predict(non_random_model, type = "response") > 0.5)

# Predict with the mixed model
# Note: For mixed models, if you want to include random effects in predictions, use re.form = NULL
df8$mixed_model_predict <- as.integer(predict(mixed_model, re.form = NULL, type = "response") > 0.5)
non_random_model_right_wrong <- c(0, 0)
mixed_model_right_wrong <- c(0, 0)

# Iterate through each row of df8
for (i in 1:nrow(df8)) {
  # For the non-random model
  if (df8$WasMondayOpenHigherThanFridayClose[i] == df8$non_random_model_predict[i]) {
    non_random_model_right_wrong[1] <- non_random_model_right_wrong[1] + 1
  } else {
    non_random_model_right_wrong[2] <- non_random_model_right_wrong[2] + 1
  }

  # For the mixed model
  if (df8$WasMondayOpenHigherThanFridayClose[i] == df8$mixed_model_predict[i]) {
    mixed_model_right_wrong[1] <- mixed_model_right_wrong[1] + 1
  } else {
    mixed_model_right_wrong[2] <- mixed_model_right_wrong[2] + 1
  }
}

# Print the results
print(non_random_model_right_wrong)
[1] 509 410
print(non_random_model_right_wrong[1] / (non_random_model_right_wrong[1] + non_random_model_right_wrong[2]))
[1] 0.5538629
print(mixed_model_right_wrong)
[1] 519 400
print(mixed_model_right_wrong[1] / (mixed_model_right_wrong[1] + mixed_model_right_wrong[2]))
[1] 0.5647443
# save df to .csv
# Save df8 to a CSV file
write.csv(df8, file = "results/SPY_non_mixed_and_mixed_model.csv", row.names = FALSE)